/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.manager;

import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.content.StopSearchRecentSuggestionsProvider;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.provider.SearchRecentSuggestions;

/**
 * <p>Stop search related preference screen manager.</p>
 * 
 * @author Justas
 * 
 */
public class StopSearchPreferenceManager extends AppPreferenceManager {

    public static final String KEY_NEARBY_STOP_RADIUS = "nearby_stop_radius";
    public static final String KEY_CLEAR_SEARCH_HISTORY = "clear_search_history";

    public StopSearchPreferenceManager(Activity activity, Object caller) {
        super(activity, caller);
    }

    /**
     * Registers listeners for various preferences
     * 
     * @param caller
     */
    public void registerListeners() {
        Preference cleanRequestCache = findPreference(KEY_CLEAR_SEARCH_HISTORY);
        cleanRequestCache.setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
                AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                builder.setIcon(android.R.drawable.ic_dialog_alert).setTitle(activity.getString(R.string.need_confirmation))
                        .setMessage(activity.getString(R.string.stop_search_preferences_clear_history_confirm)).setCancelable(false)
                        .setPositiveButton(activity.getString(R.string.yes), new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                SearchRecentSuggestions suggestions = new SearchRecentSuggestions(activity,
                                        StopSearchRecentSuggestionsProvider.AUTHORITY, StopSearchRecentSuggestionsProvider.MODE);
                                suggestions.clearHistory();
                            }

                        }).setNegativeButton(activity.getString(R.string.no), new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }

                        });
                AlertDialog alert = builder.create();
                alert.show();
                return true;
            }

        });
        
        EditTextPreference nearbyStopRadius = findPreference(KEY_NEARBY_STOP_RADIUS);
        nearbyStopRadius.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                updateNearbyStopRadiusSumary((Integer) newValue);
                return true;
            }
        });
    }
    
    public void updateNearbyStopRadiusSumary() {
        int valueToDisplay = this.<Integer>resolvePreferenceValue(StopSearchPreferenceManager.KEY_NEARBY_STOP_RADIUS);

        updateNearbyStopRadiusSumary(valueToDisplay);
    }

    protected void updateNearbyStopRadiusSumary(int valueToDisplay) {
        EditTextPreference nearbyStopRadius = findPreference(KEY_NEARBY_STOP_RADIUS);
        nearbyStopRadius.setSummary(activity.getString(R.string.stop_search_preferences_nearby_stop_radius_summary, valueToDisplay));
    }
}
